package com.sample.base.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.loyal.helper.DialogHelper
import com.loyal.helper.UIHelper
import com.loyal.kit.*
import com.sample.base.R
import com.sample.base.databinding.ActivityBarBinding

class BarActivity : AppCompatActivity() {
    private val binding by lazy { ActivityBarBinding.inflate(layoutInflater) }
    private val toolbarBinding by lazy { binding.toolbar}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        StateBarUtil.showStatusBar(this)
        toolbarBinding.toolbar.title = "Bar"
        setSupportActionBar(toolbarBinding.toolbar)
        val array = ArrayList<String>()
        for (index in 0..20) {
            array.add("$index")
        }
        binding.listView.let {
            it.adapter = ArrayAdapter(
                this, android.R.layout.simple_list_item_1, android.R.id.text1, array
            )
            it.setOnItemClickListener { parent, _, position, _ ->
                val item = parent.adapter.getItem(position) as String?
                UIHelper.toastShort(this, "" + item)
                DialogHelper.showInActivity(this, item)
            }
        }

        binding.switchStatusBar.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                StateBarUtil.hideStatusBar(this)
            } else {
                val trans = binding.switchStatusTrans.isChecked
                val color = if (trans) Color.TRANSPARENT else ContextCompat.getColor(
                    this, R.color.statusColor
                )
                StateBarUtil.setStatusBarColor(this, color)
            }
        }
        binding.switchStatusTrans.setOnCheckedChangeListener { _, isChecked ->
            val color = if (isChecked) Color.TRANSPARENT else ContextCompat.getColor(
                this, R.color.statusColor
            )
            StateBarUtil.setStatusBarColor(this, color)
        }

        binding.switchNavigation.setOnCheckedChangeListener { _, isChecked ->
            /*if (isChecked) {
                StateBarUtil.hideNavigationBar(this)
            } else {
                StateBarUtil.showNavigationBar(this)
            }*/
        }
        binding.switchAction.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                actionBar?.hide()
                supportActionBar?.hide()
            } else {
                actionBar?.show()
                supportActionBar?.show()
            }
        }
        binding.switchScreen.setOnCheckedChangeListener { _, isChecked ->
            StateBarUtil.fullScreen(this, isChecked);
            //StateBarUtil.setTranslucentStatus(this, isChecked)
        }
        binding.navBarHeight.setOnClickListener {
            val isNavigationBarShow = 0 != StateBarUtil.navBarSize(this)
            Log.e("是否显示导航栏: ", "isNavigationBarShow->$isNavigationBarShow")

            val hasNavBar = StateBarUtil.hasNavBar(this)
            Log.e("是否存在导航栏: ", "hasNavBar->$hasNavBar")
            val tintManager = SystemBarTintManager(this)
            val config = tintManager.config
            Log.e("config: ", GsonUtil.obj2Json(config))
            val width = config.navigationBarWidth
            val height = config.navigationBarHeight
            //val height = StateBarUtil.getNavBarHeight(this)
            binding.height.text = "size=$width*${height}"
        }
    }
}